#!/bin/sh
if [ ! -d "$1" ] || [ ! -n "$2" ]; then
cat<<HELP
Usage: ./guid_lasttime_update.sh sourcePath date
Example: 
1. specified date: 
    ./guid_lasttime_update.sh /data/log 20120113
2. specified date hour: 
    ./guid_lasttime_update.sh /data/log 20120113 06
HELP
    exit 1
fi

PWD="/opt/wanalytics/scripts"
TOOLS=`dirname $PWD`/Tools

YEAR=${2:0:4}
MONTH=${2:4:2}
DAY=${2:6:2}
HOUR=$3

if [ -z "$HOUR" ]; then
    HOUR="[0-9][0-9]"
fi

# 日志路径
#LOGS=$PWD/log/guid_lasttime_update.log

SOURCE_PATH=$1/$YEAR/$MONTH/$DAY
if [ ! -d "$SOURCE_PATH/_import" ]; then
    mkdir -p $SOURCE_PATH/_import
fi

# 批量更新mongo
GUID_LASTTIME_UPDATE=$SOURCE_PATH/_import/guid_lasttime_update

if [ -s $GUID_LASTTIME_UPDATE ]; then
    rm -rf $GUID_LASTTIME_UPDATE
fi
date_start=`date "+%s"`

echo "cat $SOURCE_PATH/[a-z,0-9]*/access_$HOUR |awk '{ Param = $5$6; Guid_offset = index(Param,"guid="); Pid_offset = index(Param,"pid="); if(Guid_offset && Pid_offset){ guid = substr(Param,Guid_offset+5,32); pid = substr(Param,Pid_offset+4,16); if(guid ~ /^[0-9a-z]{32}$/ && pid ~ /^[0-9a-z]{16}$/ ){print $2,guid,pid}else{next;}}else{next;}}' | sort -u -k 2 -k 3 | awk -f $PWD/guid_lasttime_update.awk -v Outfile=$GUID_LASTTIME_UPDATE"

cat $SOURCE_PATH/[a-z,0-9]*/access_$HOUR |awk '{ Param = $5$6; Guid_offset = index(Param,"guid="); Pid_offset = index(Param,"pid="); if(Guid_offset && Pid_offset){ guid = substr(Param,Guid_offset+5,32); pid = substr(Param,Pid_offset+4,16); if(guid ~ /^[0-9a-z]{32}$/ && pid ~ /^[0-9a-z]{16}$/ ){print $2,guid,pid}else{next;}}else{next;}}' | sort -u -k 2 -k 3 | awk -f $PWD/guid_lasttime_update.awk -v Outfile=$GUID_LASTTIME_UPDATE

scp $GUID_LASTTIME_UPDATE root@10.103.13.103:/tmp/

python $PWD/guid_lasttime_update.py /tmp/guid_lasttime_update $2

date_end=`date "+%s"`
time=`expr "$date_end" - "$date_start"`
echo "LastTime update take $time seconds"
